import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store";
import { getSyncRouter } from "@/utils/index";

if (window.__POWERED_BY_QIANKUN__) {
  __webpack_public_path__ = window.__INJECTED_PUBLIC_PATH_BY_QIANKUN__;
}

Vue.config.productionTip = false;

let instance = null;
function render(props = {}) {
  const { container } = props;
  instance = new Vue({
    router,
    store,
    render: (h) => h(App),
  }).$mount(container ? container.querySelector("#app") : "#app");
}

// 独立运行时
if (!window.__POWERED_BY_QIANKUN__) {
  render();
}

export async function bootstrap(props) {
  const allRouter = getSyncRouter(props.routerData, props.name);
  allRouter.map((r) => router.addRoute(r)); // 添加路由表
  Vue.prototype.$pager = props.pager; // 将呼机挂载到vue原型上
  props.pager.subscribe((v) => {
    // 在子应用注册呼机监听器，这里可以监听到其他应用的广播
    console.log(`[sub-app2] 监听到子应用${v.from}发来消息：`, v);
    // store.dispatch('app/setToken', v.token)   // 在子应用中监听到其他应用广播的消息后处理逻辑
    store.commit("SET_MSG", v.data);
  });
}
export async function mount(props) {
  console.log("[vue-app2] props from main framework", props);
  render(props);
}
export async function unmount() {
  instance.$destroy();
  instance.$el.innerHTML = "";
  instance = null;
}
